$(function () {
    // 加载学生列表
    loadTable();

    // 新增学生按钮点击事件
    $("#addStudent").click(function () {
        addStudent();
    });

    // 编辑按钮点击事件
    $(document).on("click", ".editStudent", function () {
        var studentId = $(this).data("id");
        editStudent(studentId);
    });

    // 删除按钮点击事件
    $(document).on("click", ".deleteStudent", function () {
        var studentId = $(this).data("id");
        deleteStudent(studentId);
    });
});

// 加载学生列表函数
function loadTable() {
    $.ajax({
        url: "/webapi/student/list",
        type: "GET",
        success: function (data) {
            var html = "";
            for (var i = 0; i < data.length; i++) {
                var item = data[i];

                html += "<tr>";
                html += "<td>" + item.id + "</td>";
                html += "<td>" + item.id + "</td>";
                html += "<td>" + item.no + "</td>";
                html += "<td>" + item.name + "</td>";
                html += "<td>" + item.sex + "</td>";
                html += "<td>" + item.age + "</td>";
                html += "<td>";
                html += "<a href='#' class='editStudent' data-id='" + item.id + "'>编辑/</a>";
                html += "<a href='#' class='deleteStudent' data-id='" + item.id + "'>删除</a>";
                html += "</td>";
                html += "</tr>";
            }

            $("#students").html(html);
            console.log(data);
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.error("Ajax请求失败：" + textStatus + ", " + errorThrown);
        },
    });
}

// 新增学生函数
function addStudent() {
    var formData = $("#formStudent").serialize();

    $.ajax({
        url: "/webapi/student/insert",
        method: "POST",
        data: formData,
        success: function () {
            // 新增成功后重新加载学生列表并关闭模态框
            loadTable();
            $("#myModal").modal("hide");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.error("Ajax请求失败：" + textStatus + ", " + errorThrown);
        },
    });
}

var studentData; // 全局变量，存储当前编辑的学生信息
var index; // 全局变量，存储当前编辑的行索引

function editStudent(id) {
    // 根据id获取对应的学生信息
    $.ajax({
        url: "/webapi/student/get/" + id,
        type: "GET",
        success: function (data) {
            studentData = data; // 存储学生信息

            // 将学生信息填充到对话框中
            $("#studentId").val(data.id);
            $("#myModal input[name='no']").val(data.no);
            $("#myModal input[name='name']").val(data.name);
            $("#myModal select[name='sex']").val(data.sex);
            $("#myModal input[name='age']").val(data.age);

            // 记录当前编辑的行索引
            index = $(this).closest("tr").index();

            // 显示编辑对话框
            $("#myModal").modal("show");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.error("Ajax请求失败：" + textStatus + ", " + errorThrown);
        },
    });
}


function updateStudent() {
    var formData = $("#formStudent").serialize();

    $.ajax({
        url: "/webapi/student/update",
        type: "POST",
        data: formData,
        success: function () {
            // 更新成功后直接更新表格中对应的行数据
            var tableRow = $("#students tr:eq("+index+")");
            tableRow.find("td:eq(1)").text($("#myModal input[name='no']").val());
            tableRow.find("td:eq(2)").text($("#myModal input[name='name']").val());
            tableRow.find("td:eq(3)").text($("#myModal select[name='sex']").val());
            tableRow.find("td:eq(4)").text($("#myModal input[name='age']").val());

            // 关闭模态框
            $("#myModal").modal("hide");
        },
        error: function (jqXHR, textStatus, errorThrown) {
            console.error("Ajax请求失败：" + textStatus + ", " + errorThrown);
            // 处理错误，比如弹出错误提示框
        },
    });
}

$(document).on("click", ".deleteStudent", function () {
    var studentId = $(this).data("id");
    if (confirm("确定要删除该学生吗？")) {
        deleteStudent(studentId);
    }
});

function deleteStudent(studentId) {
    $.ajax({
        url: "/webapi/student/delete/" + studentId,
        type: "DELETE",
        success: function () {
            location.reload();
        }
    });
}
// 将 updateStudent 函数绑定到保存按钮的点击事件上
$(document).on("click", "#saveStudent", function () {
    updateStudent();
});
